package com.mingmen.canting.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzy.imagepicker.bean.ImageItem;
import com.mingmen.canting.R;
import com.mingmen.canting.defineview.SquareCenterFrameLayout;
import com.mingmen.canting.model.ZhenBanShenHe;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by guohao on 2017/9/6.
 */

public class ImageListAdapter extends RecyclerView.Adapter<ImageListAdapter.ViewHolder> implements View.OnClickListener {

    private static final int MYLIVE_MODE_CHECK = 0;
    int mEditMode = MYLIVE_MODE_CHECK;

    private int secret = 0;
    private String title = "";
    private Context context;
    private ArrayList<ImageItem> images;
    private OnItemClickListener mOnItemClickListener;

    public ImageListAdapter(Context context, ArrayList<ImageItem> images) {
        this.context = context;
        this.images = images;
    }


    public void setDataList(ArrayList<ImageItem> images) {
        this.images = images;
        notifyDataSetChanged();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_img_list, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public int getItemCount() {
        return images.size();
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        Bitmap bm = BitmapFactory.decodeFile(images.get(position).path);

        holder.sdv.setImageBitmap(bm);

        holder.img_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, holder.getAdapterPosition(), images);
            }
        });
        if (images.size() > 9) {
//            holder.add_sc.setVisibility(View.GONE);
        } else {
//            holder.add_sc.setVisibility(View.VISIBLE);
        }
        holder.add_sc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mOnItemClickListener.onItemClickListener(v, holder.getAdapterPosition(), images);
            }
        });
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();

    }

    public interface OnItemClickListener {
        //        void onItemClickListener(int pos, DaishenheListAdapter.ViewName viewName, List<MyLiveList> myLiveList);
        void onItemClickListener(View view, int pos, ArrayList<ImageItem> myLiveLists);
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView sdv;
        ImageView img_icon;
        SquareCenterFrameLayout add_sc;

        public ViewHolder(View itemView) {
            super(itemView);
            sdv = itemView.findViewById(R.id.sdv);
            img_icon = itemView.findViewById(R.id.img_icon);
            add_sc = itemView.findViewById(R.id.add_sc);
        }
    }


}
